package com.lepu.app.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

import com.app.config.AppConfig;
import com.app.utils.Const;
import com.app.utils.DspTongJi;
import com.app.utils.LocationUtil;
import com.app.utils.LoginUtil;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.usercenter.LoginActivity;
import com.lepu.pasm.R;

//import com.lepu.app.main.push.Utils;

/*
 * 启动页
 */
public class AppStartActivity extends BaseFragmentActivity {

	public static final int DELAY_TIME = 1000 * 3;
	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedSwipeBack = false;
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);

		View view = View.inflate(this, R.layout.app_start_activity, null);
		setContentView(view);

		// showLogo();
		LogUtilBase.LogD(null, "AppStartActivity=====>>onCreate");
		// 启动首页进入动画
		startAnim(view);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// 如果是启动页，屏蔽返回键
	}

	private void startAnim(View view) {

		AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
		alphaAnimation.setDuration(DELAY_TIME);
		view.startAnimation(alphaAnimation);
		alphaAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				init();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}
		});
	}

	private void init() {
		// 进入程序逻辑判断
		boolean appFirstUse = AppConfig.getConfigBoolean(this,
				Const.CONFIG_APP_FIRST_USE, true);
//		boolean appUpgradeUse = AppConfig.getConfigBoolean(this,
//				Const.CONFIG_APP_UPGRADE_USE, true);

		//记录使用时间(有些用户已经安装,因此从此版本开始记录,不能从第一次安装记录)
		if(AppConfig.getConfigLong(this, Const.CONFIG_APP_USE_TIME, 0) == 0){
			AppConfig.setConfig(this, Const.CONFIG_APP_USE_TIME, DateUtilBase.getNowTimeInMillis());
		}

		Intent intent = new Intent();
		if (appFirstUse) {
			// 第一次启动程序，先检查sd卡文件
			if (appFirstUse) {
				Utils.cleanAllFile(AppStartActivity.this);

				// 只需要dsp渠道,统计程序第一次使用
				new DspTongJi().requestFirstUse(AppStartActivity.this);
			}
			intent.setClass(AppStartActivity.this, AppStartActivityGuide.class);
		} else {
			
			if(LoginUtil.checkIsLogin())
			{
				intent.setClass(AppStartActivity.this, MainTabActivity.class);
			}else{
				intent.setClass(AppStartActivity.this, LoginActivity.class);
				intent.putExtra("first_enter", true);
			}
		}
		
		startActivityAnim(intent,0,0);
		finishAnim(0,0);
	}
}
